/**
 *  Copyright 2009 Henning Teek, Ingo Renner
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 */
package com.googlecode.chigs.web.common;

import com.googlecode.chigs.domain.Module;
import com.googlecode.chigs.domain.Revision;
import com.googlecode.chigs.repository.ChigsFacade;
import com.googlecode.chigscommons.web.common.table.DataProvider;
import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

/**
 * Generische Dataprovider, der die üblichen Zugriffe auf Domainobjekte eines Typs regelt.
 * Hauptsachlich für Auflistung in Tabellen und Aufzählungen.
 * Zuerst erstellt für die Ajaxfallbacktable
 * 
 * @author Ingo Renner
 * @author Henning Teek
 *
 */
public class RevisionByModuleDataProvider extends DataProvider<Revision> {
	private final static Logger log = LoggerFactory.getLogger(RevisionByModuleDataProvider.class);

	@SpringBean
	private ChigsFacade chigsFacade;

	private final List<Revision> revisions;

	public RevisionByModuleDataProvider(Revision domainObject, Module module) {
		super(domainObject);
		revisions = chigsFacade.loadRevisionsByModule(module);
	}

	@Override
	public Iterator<Revision> iterator(int from, int count) {
		final SortParam sort = getSort();
		if (sort != null) {
			log.debug("sorting by " + sort.getProperty() + (sort.isAscending() ? " ascending" : " descending"));
			Collections.sort(revisions, new Comparator<Revision>() {
				@Override
				public int compare(Revision o1, Revision o2) {
					if ("id".equals(sort.getProperty())) {
						return sort.isAscending() ? 
								o1.getId().compareTo(o2.getId()) : 
									o2.getId().compareTo(o1.getId());
					} else if ("description".equals(sort.getProperty())) {
						return sort.isAscending() ? 
								o1.getDescription().compareTo(o2.getDescription()) : 
									o2.getDescription().compareTo(o1.getDescription());
					} else if ("nr".equals(sort.getProperty())) {
						return sort.isAscending() ? 
								Integer.valueOf(o1.getNr()).compareTo(Integer.valueOf(o2.getNr())) : 
									Integer.valueOf(o2.getNr()).compareTo(Integer.valueOf(o1.getNr()));
					} else if ("user.email".equals(sort.getProperty())) {
						return sort.isAscending() ? 
								o1.getUser().getEmail().compareTo(o2.getUser().getEmail()) : 
									o2.getUser().getEmail().compareTo(o1.getUser().getEmail());
					} else if ("version.version".equals(sort.getProperty())) {
						return sort.isAscending() ? 
								o1.getVersion().getVersion().compareTo(o2.getVersion().getVersion()) : 
									o2.getVersion().getVersion().compareTo(o1.getVersion().getVersion());
					}
					return 0;
				}
			});
		}
		return revisions.subList(from, from + count).iterator();
	}

	@Override
	public int size() {
		return revisions.size();
	}
}
